// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


//
// Created by Ying Su on 2/14/22.
//

#include <pollux/dwio/parquet/reader/parquet_column_reader.h>

#include <pollux/dwio/common/selective_column_reader_internal.h>
#include <pollux/dwio/parquet/reader/boolean_column_reader.h>
#include <pollux/dwio/parquet/reader/floating_point_column_reader.h>
#include <pollux/dwio/parquet/reader/integer_column_reader.h>
#include <pollux/dwio/parquet/reader/repeated_column_reader.h>
#include <pollux/dwio/parquet/reader/string_column_reader.h>
#include <pollux/dwio/parquet/reader/struct_column_reader.h>
#include <pollux/dwio/parquet/reader/timestamp_column_reader.h>
#include <pollux/dwio/parquet/thrift/ParquetThriftTypes.h>

namespace kumo::pollux::parquet {
    // static
    std::unique_ptr<dwio::common::SelectiveColumnReader> ParquetColumnReader::build(
        const TypePtr &requestedType,
        const std::shared_ptr<const dwio::common::TypeWithId> &fileType,
        ParquetParams &params,
        common::ScanSpec &scanSpec) {
        auto colName = scanSpec.fieldName();

        switch (fileType->type()->kind()) {
            case TypeKind::INTEGER:
            case TypeKind::BIGINT:
            case TypeKind::SMALLINT:
            case TypeKind::TINYINT:
            case TypeKind::HUGEINT:
                return std::make_unique<IntegerColumnReader>(
                    requestedType, fileType, params, scanSpec);

            case TypeKind::REAL:
                return std::make_unique<FloatingPointColumnReader<float, float> >(
                    requestedType, fileType, params, scanSpec);
            case TypeKind::DOUBLE:
                return std::make_unique<FloatingPointColumnReader<double, double> >(
                    requestedType, fileType, params, scanSpec);

            case TypeKind::ROW:
                return std::make_unique<StructColumnReader>(
                    requestedType, fileType, params, scanSpec);

            case TypeKind::VARBINARY:
            case TypeKind::VARCHAR:
                return std::make_unique<StringColumnReader>(fileType, params, scanSpec);

            case TypeKind::ARRAY:
                return std::make_unique<ListColumnReader>(
                    requestedType, fileType, params, scanSpec);

            case TypeKind::MAP:
                return std::make_unique<MapColumnReader>(
                    requestedType, fileType, params, scanSpec);

            case TypeKind::BOOLEAN:
                return std::make_unique<BooleanColumnReader>(
                    requestedType, fileType, params, scanSpec);

            case TypeKind::TIMESTAMP: {
                const auto parquetType =
                        std::static_pointer_cast<const ParquetTypeWithId>(fileType)
                        ->parquetType_;
                POLLUX_CHECK(parquetType);
                switch (parquetType.value()) {
                    case thrift::Type::INT64:
                        return std::make_unique<TimestampColumnReader<int64_t> >(
                            requestedType, fileType, params, scanSpec);
                    case thrift::Type::INT96:
                        return std::make_unique<TimestampColumnReader<int128_t> >(
                            requestedType, fileType, params, scanSpec);
                    default:
                        POLLUX_UNREACHABLE();
                }
            }

            default:
                POLLUX_FAIL(
                    "buildReader unhandled type: " +
                    mapTypeKindToName(fileType->type()->kind()));
        }
    }
} // namespace kumo::pollux::parquet
